<?php
namespace Home\Model;
use Think\Model;
use Common\Common\Classes\Message;

class StudentQuestionService extends Model{
    private $sq_sql;
    public function __construct(){
        $this->sq_sql = D("StudentQuestion");
    }

    //---------------------------------获得某个题目的正确率-----------------------------------------------
    public function countQuestionAccuracy(){            //题目id
        $submitNum = $this->sq_sql->where($_POST)->count();
        $_POST["is_correct"] = 1;
        $correctNum = $this->sq_sql->where($_POST)->count();
        unset($_POST["is_correct"]);            //消除,不然会影响下一个的计算
        $accuracy = (round($correctNum / $submitNum,4)*100)."%";        //计算正确率
        return $accuracy;
    }
    //---------------------------------获得某个题目选项的提交人数-----------------------------------------------
    public function countItemSubmitNum(){       //题目id 和 stu_answer
        $submitNum = $this->sq_sql->where($_POST)->count();
        unset($_POST["stu_answer"]);
        return $submitNum;
    }
    //---------------------------------获得某个题目选项的提交人数的百分比-----------------------------------------------
    public function countItemPer($submitNum){
        $per = (round($submitNum/$this->sq_sql->where($_POST)->count(),4)*100)."%";
        return $per;
    }
    //---------------------------------获得学生的答题情况(做题情况)--------------------------------------
    public function getStuQuizResult($field){
        $result = $this->sq_sql->where($_POST)->field($field)->find();
        if($result == null){
            $result["stu_answer"] = null;
            $result["is_correct"] = 0;
        }
        return $result;
    }
    //---------------------------------处理所有题目的格式(捎去null)---------------------------------
    public function unsetQusetion($questions){
        for($i = 0;$i<count($questions);$i++){
            for($j = 0;$j<8;$j++){
                if($questions[$i]["que_item".($j+1)] != null){          //清除null的选项
                    $questions[$i]["que_item"][$j] = array("content" => $questions[$i]["que_item".($j+1)]);
                }
                unset($questions[$i]["que_item".($j+1)]);
            }
        }
        return $questions;
    }
    public function __destruct(){}
}